package com.qf.kady.ssm.controller;

import com.qf.kady.ssm.entity.ResultVO;
import com.qf.kady.ssm.mapper.ReaderMapper;
import com.qf.kady.ssm.pojo.Book;
import com.qf.kady.ssm.pojo.DropLog;
import com.qf.kady.ssm.pojo.Reader;
import com.qf.kady.ssm.pojo.User;
import com.qf.kady.ssm.service.BookService;
import com.qf.kady.ssm.service.DropLogService;
import com.qf.kady.ssm.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/4/21
 * \* Time: 11:50
 * \* 千峰JAVA2101班
 * \* Description: 读者页面控制层
 * \
 */

@Controller
@RequestMapping("/reader")
public class ReaderPageController {

    @Autowired
    private ReaderService readerService;

    @Autowired
    private BookService bookService;

    @Autowired
    private DropLogService dropLogService;

    @RequestMapping("/login")
    @ResponseBody
    public ResultVO login(@RequestBody User user, HttpSession session){
        Reader reader = new Reader();
        reader.setReaderName(user.getLoginName());
        reader.setPassword(user.getPassword());
        ResultVO vo = readerService.login(reader);
        session.setAttribute("logUserInfo",vo.isSuccess()?((Reader)(vo.getData())):null);
        return vo;
    }

    @GetMapping("/reader_main.html")
    public ModelAndView toReaderMain(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("reader/reader_main");
        return mav;
    }

    @GetMapping("/logout.html")
    public String logOut(HttpSession session){
        session.removeAttribute("logUserInfo");
        return "redirect:/reader/reader_main.html";
    }

    @GetMapping("/reader_header.html")
    public ModelAndView toReaderHeader(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("reader/reader_header");
        return mav;
    }

    @RequestMapping({"/reader_books.html", "/reader_querybook_do.html"})
    public ModelAndView toReaderBooks(String searchWord,Long readerId){
        List<Book> list = bookService.getBookListByCondition(searchWord, null, readerId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("books",list);
        mav.addObject("keyword",searchWord);
        mav.setViewName("reader/reader_books");
        return mav;
    }

    @RequestMapping("/returnbook.html")
    public ModelAndView returnBook(String searchWord, Long bookId, Long readerId, Model model){
        DropLog dropLog = new DropLog();
        dropLog.setBookId(bookId);
        dropLog.setReaderId(readerId);
        ResultVO resultVO = bookService.updateBookAndDropLog(dropLog,1L);
        if(resultVO.isSuccess()){
            model.addAttribute("succ",resultVO.getMessage());
        }else{
            model.addAttribute("error",resultVO.getMessage());
        }
        return toReaderBooks(searchWord,readerId);
    }

    @RequestMapping("/lendbook.html")
    public ModelAndView lendbook(String searchWord, Long bookId, Long readerId, Model model){
        DropLog dropLog = new DropLog();
        dropLog.setBookId(bookId);
        dropLog.setReaderId(readerId);
        dropLog.setLendDate(new Date());
        ResultVO resultVO = bookService.updateBookAndDropLog(dropLog,-1L);
        if(resultVO.isSuccess()){
            model.addAttribute("succ",resultVO.getMessage());
        }else{
            model.addAttribute("error",resultVO.getMessage());
        }
        return toReaderBooks(searchWord,readerId);
    }

    @RequestMapping("/reader_book_detail.html")
    public ModelAndView toReaderBookDetail(Long bookId){
        Book book = bookService.getBookDetailById(bookId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("detail",book);
        mav.setViewName("reader/reader_book_detail");
        return mav;
    }

    @GetMapping("/reader_info.html")
    public ModelAndView toReaderInfo(Long readerId){
        Reader reader = readerService.getReaderDetailById(readerId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("readerinfo",reader);
        mav.setViewName("reader/reader_info");
        return mav;
    }

    @GetMapping("/reader_info_edit.html")
    public ModelAndView toReaderInfEdit(Long readerId){
        Reader reader = readerService.getReaderDetailById(readerId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("readerinfo",reader);
        mav.setViewName("reader/reader_info_edit");
        return mav;
    }

    @RequestMapping("/reader_edit_do_r.html")
    public ModelAndView toReaderInfoEdit(Reader reader,Model model){
        boolean res = readerService.updateReader(reader);
        if(res){
            model.addAttribute("succ","修改成功");
        }else{
            model.addAttribute("error","修改失败");
        }
        return toReaderInfo(reader.getReaderId());
    }

    @GetMapping("/mylend.html")
    public ModelAndView toMyLend(Long readerId){
        List<Book> list = bookService.getBookListByCondition(null, readerId,null);
        ModelAndView mav = new ModelAndView();
        mav.addObject("list",list);
        mav.setViewName("reader/reader_lend_list");
        return mav;
    }

    @GetMapping("/reader_repasswd.html")
    public ModelAndView toReaderRePassWd(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("reader/reader_repasswd");
        return mav;
    }

    @RequestMapping("/reader_repasswd_do")
    public ModelAndView changePassword(Long readerId, String oldPasswd, String newPasswd, String reNewPasswd, Model model){
        if(oldPasswd==newPasswd){
            model.addAttribute("error","新老密码不能相同！");
            model.addAttribute("oldPasswd",oldPasswd);
            model.addAttribute("newPasswd",newPasswd);
            return toReaderRePassWd();
        }else{
            ResultVO resultVO = readerService.changeWD(readerId, oldPasswd, newPasswd);
            if(resultVO.isSuccess()){
                model.addAttribute("succ",resultVO.getMessage());
            }else{
                model.addAttribute("error",resultVO.getMessage());
                model.addAttribute("oldPasswd",oldPasswd);
                model.addAttribute("newPasswd",newPasswd);
            }
            return toReaderRePassWd();
        }
    }

}
